class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        unordered_map<int, int> m;
        int left = 0, right = 0, ret = 0;
        for (; right < fruits.size(); right++)
        {
            m[fruits[right]]++;
            while (m.size() > 2)
            {
                m[fruits[left]]--;
                if (m[fruits[left]] == 0)
                {
                    m.erase(fruits[left]);
                }
                left++;
            }
            ret = max(ret, right - left + 1);
        }
        return ret;
    }
};